{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, BatchNormalization, Average\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### graph 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 10002\n",
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "input_layer_0 = Input(shape=data_in_shape)\n",
    "branch_0 = Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True)(input_layer_0)\n",
    "\n",
    "input_layer_1 = Input(shape=data_in_shape)\n",
    "branch_1 = Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True)(input_layer_1)\n",
    "\n",
    "output_layer = Average()([branch_0, branch_1])\n",
    "model = Model(inputs=[input_layer_0, input_layer_1], outputs=output_layer)\n",
    "\n",
    "data_in = []\n",
    "for i in range(2):\n",
    "    np.random.seed(random_seed + i)\n",
    "    data_in.append(np.expand_dims(2 * np.random.random(data_in_shape) - 1, axis=0))\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(data_in)\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = [format_decimal(data_in[i].ravel().tolist()) for i in range(2)]\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['graph_02'] = {\n",
    "    'inputs': [{'data': data_in_formatted[i], 'shape': data_in_shape} for i in range(2)],\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/graph/02.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"graph_02\": {\"inputs\": [{\"data\": [-0.096224, 0.673607, -0.009657, 0.52201, -0.470713, 0.710414, -0.62915, 0.836049, 0.958218, 0.675545, 0.87071, -0.765923, 0.270454, -0.605797, -0.781502, -0.158478, 0.942449, -0.098396, 0.479279, -0.663835, -0.216723, -0.551944, 0.672006, 0.615437, 0.936846, 0.765668, -0.725233, -0.59916, -0.233731, 0.031473, 0.927414, 0.759562, 0.97423, 0.419831, 0.318504, -0.031154, 0.959492, -0.136413, 0.062065, -0.31904, -0.460297, -0.691724, 0.576339, 0.606164, 0.677099, -0.821884, 0.706775, 0.598279, -0.373949, -0.663068, 0.974704, -0.157164, -0.934793, 0.745087, -0.871081, -0.580079, -0.015164, -0.319471, -0.336323, 0.227711, 0.345044, 0.021435, 0.742563, 0.859598, -0.887057, -0.354838, 0.668705, -0.308794, 0.971958, -0.477421, 0.436958, 0.606519, -0.24108, 0.81307, -0.945765, -0.34327, 0.715052, -0.497423, 0.816045, 0.822065, 0.506868, -0.851311, 0.738795, 0.67809, -0.644936, -0.587803, -0.59148, -0.156544, 0.353301, 0.907141, -0.404002, 0.865169, 0.93593, -0.265458, -0.604581, -0.908579, -0.59238, -0.2719, -0.254299, -0.628083, -0.39186, 0.647615, 0.880208, 0.326923, 0.393419, 0.45855, -0.765769, -0.658543, 0.884576, 0.299188, -0.826467, -0.342861, 0.566709, -0.196027, -0.030079, 0.274921, -0.332923, 0.012984, 0.554705, 0.975116, 0.010699, -0.178185, -0.972592, -0.482001, -0.31068, 0.678962, 0.670572, 0.120115], \"shape\": [8, 8, 2]}, {\"data\": [0.841248, 0.161158, 0.958983, 0.490417, 0.824656, 0.052908, 0.775934, 0.147982, -0.867877, -0.555668, -0.331903, 0.40502, -0.595569, -0.471818, -0.559532, -0.57215, -0.89974, -0.573032, -0.35398, -0.705059, -0.927461, -0.650224, -0.563291, -0.79357, 0.061125, 0.136481, -0.97837, 0.072996, -0.878323, 0.62697, -0.667599, -0.553128, 0.009751, -0.264958, -0.054374, -0.140509, 0.513801, 0.652987, 0.027122, 0.024481, -0.967907, 0.249529, 0.127448, -0.037277, -0.270137, 0.320024, -0.432672, 0.754105, -0.324482, 0.154028, -0.336313, -0.608767, 0.80611, -0.203209, -0.131729, 0.218894, -0.561637, -0.664737, 0.711306, 0.08824, -0.771977, 0.860109, 0.782578, -0.50916, -0.445579, -0.333019, 0.295793, 0.565551, -0.946729, -0.858205, -0.673907, -0.061506, -0.579362, 0.140314, -0.46189, 0.959434, -0.90153, -0.331571, 0.02769, -0.090527, -0.646693, 0.697831, 0.327324, 0.953899, 0.434714, -0.018754, 0.679171, 0.450538, 0.541374, -0.928426, 0.595931, -0.575864, -0.733921, -0.429094, -0.522865, -0.612979, -0.265133, 0.961394, 0.327196, 0.305523, -0.361009, 0.755892, -0.38218, -0.424697, 0.478128, -0.304299, -0.844554, 0.158311, 0.435603, -0.562215, -0.722012, -0.087758, -0.46262, -0.443516, 0.190205, -0.135997, -0.768256, 0.611786, 0.763558, -0.818832, 0.090036, 0.860763, 0.641142, -0.294615, -0.63098, 0.80676, -0.158879, -0.224822], \"shape\": [8, 8, 2]}], \"weights\": [{\"data\": [-0.096224, 0.673607, -0.009657, 0.52201, -0.470713, 0.710414, -0.62915, 0.836049, 0.958218, 0.675545, 0.87071, -0.765923, 0.270454, -0.605797, -0.781502, -0.158478, 0.942449, -0.098396, 0.479279, -0.663835, -0.216723, -0.551944, 0.672006, 0.615437, 0.936846, 0.765668, -0.725233, -0.59916, -0.233731, 0.031473, 0.927414, 0.759562, 0.97423, 0.419831, 0.318504, -0.031154, 0.959492, -0.136413, 0.062065, -0.31904, -0.460297, -0.691724, 0.576339, 0.606164, 0.677099, -0.821884, 0.706775, 0.598279, -0.373949, -0.663068, 0.974704, -0.157164, -0.934793, 0.745087, -0.871081, -0.580079, -0.015164, -0.319471, -0.336323, 0.227711, 0.345044, 0.021435, 0.742563, 0.859598, -0.887057, -0.354838, 0.668705, -0.308794, 0.971958, -0.477421, 0.436958, 0.606519], \"shape\": [3, 3, 2, 4]}, {\"data\": [0.841248, 0.161158, 0.958983, 0.490417], \"shape\": [4]}, {\"data\": [-0.592446, -0.52773, 0.08531, -0.949905, -0.127145, 0.411354, 0.419711, -0.622227, 0.872841, -0.88662, 0.701828, 0.153318, 0.327738, 0.178798, 0.813647, -0.366809, 0.712692, 0.986871, 0.668377, 0.736488, -0.264375, 0.19263, -0.308472, 0.407634, -0.951991, 0.764595, -0.73637, 0.313222, 0.629502, 0.444145, -0.198403, 0.174327, -0.411546, 0.35668, -0.479344, -0.451058, 0.094248, -0.172221, 0.8534, 0.999072, 0.705663, 0.149181, -0.316913, 0.880756, 0.17336, 0.883104, -0.88683, -0.455842, -0.982796, -0.645087, -0.728562, -0.492119, -0.941125, -0.696325, 0.703916, 0.751858, -0.828058, 0.145984, 0.967902, 0.566607, 0.620443, 0.060608, 0.960336, 0.077866, -0.260331, -0.995759, 0.872716, 0.516793, -0.53123, 0.709423, -0.436639, 0.143448], \"shape\": [3, 3, 2, 4]}, {\"data\": [-0.905266, -0.940244, 0.975308, -0.726779], \"shape\": [4]}], \"expected\": {\"data\": [0.0, 0.404614, 1.973704, 0.021823, 0.0, 0.0, 2.304156, 1.062756, 0.792051, 0.0, 1.081686, 0.438897, 2.389093, 1.041389, 0.446938, 0.0, 0.30515, 1.590241, 1.522165, 0.493498, 0.0, 0.0, 2.13044, 0.37904, 1.898498, 0.769325, 0.590531, 1.05498, 1.189074, 0.407704, 1.658245, 0.150179, 1.123487, 1.334194, 0.0, 0.0, 0.740703, 0.408938, 0.183377, 1.186247, 0.0, 1.077522, 0.361388, 1.057809, 1.063996, 0.0, 2.153856, 0.54458, 2.171033, 0.876541, 0.914553, 0.0, 1.329355, 0.571052, 0.256666, 0.0, 0.0, 0.0, 1.667577, 1.605327, 0.197564, 0.856822, 0.909335, 0.566077, 0.689612, 0.399442, 0.653807, 0.0, 1.129642, 0.0, 2.494566, 2.122655, 0.352292, 0.0, 4.599329, 1.516806, 0.049169, 0.348621, 0.913603, 0.6507, 1.824533, 0.0, 1.87248, 0.50016, 1.810543, 0.121646, 1.105761, 0.419098, 0.0, 0.0, 1.678942, 0.438925, 0.605008, 0.0, 0.661171, 0.442331, 2.926666, 0.857129, 1.402955, 0.397374, 0.980514, 0.695572, 2.013117, 0.310908, 0.073711, 0.0, 1.081629, 2.02513, 0.355588, 0.928746, 0.0, 0.949284, 0.0, 0.0, 0.619847, 0.252379, 1.683274, 1.000989, 1.722157, 0.129001, 1.345601, 1.360834, 1.558567, 0.153878, 0.234045, 0.0, 2.298055, 1.368595, 0.873551, 0.0, 3.392664, 1.502125, 0.839588, 0.843704, 0.948074, 0.0, 0.396067, 0.0, 0.617226, 1.394339, 1.116915, 1.269482, 0.324161, 0.055432], \"shape\": [6, 6, 4]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
